<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script src="stack.js"></script>
  <script>
    const templs = [73, 74, 75, 71, 69, 72, 76, 73]

    // => [1, 1, 4, 2, 1, 1, 0, 0]

    function change(templs) {
      // 创建一个栈
      const stack = new Stack();
      const len = templs.length;
      const arr = []; // 存放结果的数组
      // 遍历templs
      for (let i = 0; i < len; i++) {
        let j = i;
        while (j < len && templs[j] <= templs[i]) {
          stack.push(templs[j]);
          j++;
        }
        if (j === len) {
          arr.push(0)
        } else {
          arr.push(stack.len())
        }
        // 清空栈
        stack.clear();
      }
      return arr;
    }
    console.log(change(templs));
  </script>
</body>

</html>